package com.exsun.ex1077.ui.popup

import android.content.Context
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.utils.divider
import com.drake.brv.utils.grid
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.exsun.ex1077.R
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.model.Icon
import com.lxj.xpopup.core.BottomPopupView
import com.zhangping.fastjetpack.callback.OneCallback
import com.zhangping.fastjetpack.extend.toDrawableId
import kotlinx.android.synthetic.main.popop_icon_select.view.*

/**
 * 描述：图标选择弹窗
 * 作者：ZhangPing
 * 创建时间：2023/4/6 10:13
 */
class IconSelectPopup(context: Context,val onSelectListener: OneCallback<Icon>): BottomPopupView(context){

    override fun getImplLayoutId() = R.layout.popop_icon_select

    private val iconMap = hashMapOf(
        Pair(Constant.OBJECT_TYPE_VEHICLE, arrayListOf(
            Icon("taxi", "object_taxi".toDrawableId(), "object_taxi_active".toDrawableId()),
            Icon("bus", "object_bus".toDrawableId(), "object_bus_active".toDrawableId()),
            Icon("truck", "object_truck".toDrawableId(), "object_truck_active".toDrawableId()),
            Icon("car", "object_car".toDrawableId(), "object_car_active".toDrawableId()),
            Icon("tractor", "object_tractor".toDrawableId(), "object_tractor_active".toDrawableId())
        )),
        Pair(Constant.OBJECT_TYPE_SHIP, arrayListOf(
            Icon("shipone", "object_shipone".toDrawableId(), "object_shipone_active".toDrawableId()),
            Icon("shiptwo", "object_shiptwo".toDrawableId(), "object_shiptwo_active".toDrawableId()),
            Icon("shipthree", "object_shipthree".toDrawableId(), "object_shipthree_active".toDrawableId()),
            Icon("shipfour", "object_shipfour".toDrawableId(), "object_shipfour_active".toDrawableId()),
        )),
        Pair(Constant.OBJECT_TYPE_PERSON, arrayListOf(
            Icon("personone", "object_personone".toDrawableId(), "object_personone_active".toDrawableId()),
            Icon("persontwo", "object_persontwo".toDrawableId(), "object_persontwo_active".toDrawableId()),
            Icon("personthree", "object_personthree".toDrawableId(), "object_personthree_active".toDrawableId()),
            Icon("personfour", "object_personfour".toDrawableId(), "object_personfour_active".toDrawableId()),
        )),
        Pair(Constant.OBJECT_TYPE_OBJECT, arrayListOf(
            Icon("goodsone", "object_goodsone".toDrawableId(), "object_goodsone_active".toDrawableId()),
            Icon("goodstwo", "object_goodstwo".toDrawableId(), "object_goodstwo_active".toDrawableId()),
            Icon("goodsthree", "object_goodsthree".toDrawableId(), "object_goodsthree_active".toDrawableId()),
            Icon("goodsfour", "object_goodsfour".toDrawableId(), "object_goodsfour_active".toDrawableId()),
            Icon("goodsfive", "object_goodsfive".toDrawableId(), "object_goodsfive_active".toDrawableId()),
            Icon("goodssix", "object_goodssix".toDrawableId(), "object_goodssix_active".toDrawableId()),
            Icon("goodsseven", "object_goodsseven".toDrawableId(), "object_goodsseven_active".toDrawableId()),
        )),
        Pair(Constant.OBJECT_TYPE_ANIMAL, arrayListOf(
            Icon("animalone", "object_animalone".toDrawableId(), "object_animalone_active".toDrawableId()),
            Icon("animaltwo", "object_animaltwo".toDrawableId(), "object_animaltwo_active".toDrawableId()),
            Icon("animalthree", "object_animalthree".toDrawableId(), "object_animalthree_active".toDrawableId()),
            Icon("animalfour", "object_animalfour".toDrawableId(), "object_animalfour_active".toDrawableId()),
            Icon("animalfive", "object_animalfive".toDrawableId(), "object_animalfive_active".toDrawableId()),
        )),
        Pair(Constant.OBJECT_TYPE_OTHER, arrayListOf(
            Icon("othertype", "object_othertype".toDrawableId(), "object_othertype_active".toDrawableId())
        ))
    )
    private var iconType: Int = Constant.OBJECT_TYPE_VEHICLE

    override fun onCreate() {
        super.onCreate()
        iconListView.grid(5).divider {
            orientation = DividerOrientation.GRID
            setDivider(20, true)
        }.setup {
            addType<Icon> { R.layout.item_icon_select }
            onClick(R.id.rootView){
                mutable.forEachIndexed { index, icon ->
                    (icon as Icon).checked = index == modelPosition
                }
                notifyDataSetChanged()
                dismiss()
                onSelectListener.onCall(getModel())
            }
        }.models = iconMap[iconType]
    }

    fun show(type: Int){
        this.iconType = type
        iconListView?.models = iconMap[type]
        show()
    }

}